import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.text.DateFormat;
import java.util.Locale;

public class ResultsPrint extends JDialog
{
    public ResultsPrint(JFrame frame, Patient patient)
    {
        super(frame,Resources.getString("new"),true);
        diagnostic = "";
        setLayout(new BorderLayout());
        JPanel panelBackground = new JPanel(new FlowLayout());
        JPanel panelBackground_2 = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(null);
        panel.setPreferredSize( new Dimension( TEXT_WIDTH, TEXT_HEIGHT ) );  
        textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setMargin(new Insets(20,20,20,20));
        textPane.setStyledDocument(
            new DefaultStyledDocument());
        StyledDocument doc = textPane.getStyledDocument();
        setDataDocument(doc, patient);
        JScrollPane scroll = new JScrollPane(
                textPane, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setBounds( 0, 0, TEXT_WIDTH, TEXT_HEIGHT ); 
        panel.add(scroll);
        panelBackground.add(panel);
        panelBackground_2.add(panelBackground,BorderLayout.CENTER);
        panelBackground_2.add(getTaskPanel(),BorderLayout.SOUTH);
        add(panelBackground_2,BorderLayout.NORTH);
        this.setSize(WIDTH, HEIGHT);
        this.setResizable(false);
    }
    public ResultsPrint(JFrame frame, Patient patient,String diagnostic_)
    {
        super(frame,Resources.getString("new"),true);
        diagnostic = diagnostic_;//.replaceAll(System.getProperty("line.separator"), "");
        setLayout(new BorderLayout());
        JPanel panelBackground = new JPanel(new FlowLayout());
        JPanel panelBackground_2 = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(null);
        panel.setPreferredSize( new Dimension( TEXT_WIDTH, TEXT_HEIGHT ) );  
        textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setMargin(new Insets(20,20,20,20));
        textPane.setStyledDocument(
            new DefaultStyledDocument());
        StyledDocument doc = textPane.getStyledDocument();
        setDataDocument(doc, patient);
        JScrollPane scroll = new JScrollPane(
                textPane, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setBounds( 0, 0, TEXT_WIDTH, TEXT_HEIGHT ); 
        panel.add(scroll);
        panelBackground.add(panel);
        panelBackground_2.add(panelBackground,BorderLayout.CENTER);
        panelBackground_2.add(getTaskPanel(),BorderLayout.SOUTH);
        add(panelBackground_2,BorderLayout.NORTH);
        this.setSize(WIDTH, HEIGHT);
        this.setResizable(false);
    }
    public ResultsPrint(JFrame frame, Patient patient,String diagnostic_,String title)
    {
    	super(frame,Resources.getString("print")+" "+title,true);
        diagnostic = diagnostic_;//.replaceAll(System.getProperty("line.separator"), "");
        setLayout(new BorderLayout());
        JPanel panelBackground = new JPanel(new FlowLayout());
        JPanel panelBackground_2 = new JPanel(new BorderLayout());
        JPanel panel = new JPanel(null);
        panel.setPreferredSize( new Dimension( TEXT_WIDTH, TEXT_HEIGHT ) );  
        textPane = new JTextPane();
        textPane.setEditable(false);
        textPane.setMargin(new Insets(20,20,20,20));
        textPane.setStyledDocument(
            new DefaultStyledDocument());
        StyledDocument doc = textPane.getStyledDocument();
        setDataDocument(doc, patient);
        JScrollPane scroll = new JScrollPane(
                textPane, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setBounds( 0, 0, TEXT_WIDTH, TEXT_HEIGHT ); 
        panel.add(scroll);
        panelBackground.add(panel);
        panelBackground_2.add(panelBackground,BorderLayout.CENTER);
        panelBackground_2.add(getTaskPanel(),BorderLayout.SOUTH);
        add(panelBackground_2,BorderLayout.NORTH);
        this.setSize(WIDTH, HEIGHT);
        this.setResizable(false);
    }
    public void setDataDocument(StyledDocument doc, Patient patient)
    {
        String stringAux;
        double endo = NutritionForm.endomorphy(
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
            patient.getAnthropometryValue(ID.A_CREASE.SUBSCAPULARIS),
            patient.getAnthropometryValue(ID.A_CREASE.SUPRAILIAC)
        );
        double meso = NutritionForm.mesomorphy(
            patient.getAnthropometryValue(ID.A_DIAMETER.HUMERAL),
            patient.getAnthropometryValue(ID.A_DIAMETER.FEMORAL),
            patient.getAnthropometryValue(ID.A_GIRTH.CONTRACTED_ARM),
            patient.getAnthropometryValue(ID.A_GIRTH.LEG),
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A_CREASE.TRICEP),
            patient.getAnthropometryValue(ID.A_CREASE.LEG)
        );
        double ecto = NutritionForm.ectomorphy(
            patient.getAnthropometryValue(ID.A.SIZE),
            patient.getAnthropometryValue(ID.A.CURRRENT_WEIGHT)
        );
        Somatotype sportSoma = SomatotypeIO.getSomatotype( patient.getSport(), patient.getGender());
        try{
            SimpleAttributeSet attrs = new SimpleAttributeSet();
            StyleConstants.setFontFamily(attrs,Font.SANS_SERIF);
            StyleConstants.setFontSize(attrs,12);
            StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_JUSTIFIED);
            StyleConstants.setBold( attrs, true);
            stringAux = Resources.getString("evaluation")+"\n";
            doc.setParagraphAttributes(
                doc.getLength(),
                stringAux.length(),
                attrs,
                false);
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
            StyleConstants.setBold( attrs, false);
            StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_RIGHT);
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("es","MX"));
            stringAux = Resources.getString("date")+": "+df.format(patient.getDate())+"\n";
            doc.setParagraphAttributes(
                doc.getLength(),
                stringAux.length(),
                attrs,
                false);
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
            StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_JUSTIFIED);
            stringAux = Resources.getString("name")+": "+patient.getName()+" "+patient.getLastName()+"\n";
            doc.setParagraphAttributes(
                doc.getLength(),
                stringAux.length(),
                attrs,
                false);
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
            
            stringAux = Resources.getString("age")+": "+Methods.calculateAge(patient.getDate(),patient.getDateBirth())+"\n";
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
                
            stringAux = Resources.getString("sport")+": "+patient.getSport()+"\n";
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
                
            stringAux = Resources.getString("category")+": "+patient.getCategory()+"\n";
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
                
            stringAux = Resources.getString("coachsName")+": "+patient.getCoachsName()+"\n";
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
                
            stringAux = Resources.getString("institution")+": "+patient.getInstitution()+"\n";
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
                
            StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_CENTER);
            stringAux = "\n"+Resources.getString("somatochart")+"\n";
            doc.setParagraphAttributes(
                doc.getLength(),
                1,
                attrs,
                false);
            
            textPane.setCaretPosition(textPane.getStyledDocument().getLength());
            Somatochart somatochart = new Somatochart();
            somatochart.addPoint(sportSoma.getEndo(),sportSoma.getMeso(),sportSoma.getEcto(),patient.getSport());
            somatochart.addPoint(endo,meso,ecto,patient.getName());
            textPane.insertIcon(new ImageIcon(somatochart.getImage().getScaledInstance(350,350,Image.SCALE_SMOOTH)));
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
            
            StyleConstants.setAlignment(attrs, StyleConstants.ALIGN_JUSTIFIED);
            stringAux = "\n"+patient.getName()+"\n"
                        +Resources.getString("endomorphy")+" = "+Methods.round(endo,3)+"\n"
                        +Resources.getString("mesomorphy")+" = "+Methods.round(meso,3)+"\n"
                        +Resources.getString("ectomorphy")+" = "+Methods.round(ecto,3)+"\n\n"
                        +patient.getSport()+"\n"
                        +Resources.getString("endomorphy")+" = "+sportSoma.getEndo()+"\n"
                        +Resources.getString("mesomorphy")+" = "+sportSoma.getMeso()+"\n"
                        +Resources.getString("ectomorphy")+" = "+sportSoma.getEcto()+"\n";
            doc.setParagraphAttributes(
                doc.getLength(),
                stringAux.length(),
                attrs,
                false);
            doc.insertString(
                doc.getLength(), 
                stringAux, 
                attrs);
            
            if(!diagnostic.equals(""))
            {
                stringAux = "\n"+Resources.getString("diagnostic")+"\n\n"+diagnostic;
                doc.insertString(
                    doc.getLength(), 
                    stringAux, 
                    attrs);
            }
            
        }catch(BadLocationException e){}
    }
    private JPanel getTaskPanel()
    {
        JPanel panel = new JPanel();
        JButton printButton = new JButton(Resources.getString("print"));
        printButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        JButton cancelButton = new JButton(Resources.getString("cancel"));
        cancelButton.setFont(Resources.getFont(Resources.FONT_TYPE.LABELS));
        printButton.addActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                	printText();
                	ResultsPrint.this.dispose();
                }
            }
        );
        cancelButton.addActionListener( new 
            ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    ResultsPrint.this.dispose();
                }
            }
        );
        panel.add(printButton);
        panel.add(cancelButton);
        return panel;
    }
    private void printText()
    {
    	HashPrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.NA_LETTER);
        aset.add(PrintQuality.NORMAL);
        aset.add(Chromaticity.MONOCHROME);
        try
        {
            textPane.print(null,null,true,null,aset,true);
        } catch(PrinterException exception){}
    }
    private JTextPane textPane;
    private String diagnostic;
    private static final int HEIGHT = 600;
    private static final int WIDTH  = 800;
    private static final int TEXT_HEIGHT = 500;
    private static final int TEXT_WIDTH  = 600;
}
